package cn.iocoder.yudao.module.personal.controller.admin.notetag;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.personal.controller.admin.notetag.vo.*;
import cn.iocoder.yudao.module.personal.dal.dataobject.notetag.NoteTagDO;
import cn.iocoder.yudao.module.personal.service.notetag.NoteTagService;

@Tag(name = "管理后台 - 笔记标签")
@RestController
@RequestMapping("/personal/note-tag")
@Validated
public class NoteTagController {

    @Resource
    private NoteTagService noteTagService;

    @PostMapping("/create")
    @Operation(summary = "创建笔记标签")
    @PreAuthorize("@ss.hasPermission('personal:note-tag:create')")
    public CommonResult<Integer> createNoteTag(@Valid @RequestBody NoteTagSaveReqVO createReqVO) {
        return success(noteTagService.createNoteTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新笔记标签")
    @PreAuthorize("@ss.hasPermission('personal:note-tag:update')")
    public CommonResult<Boolean> updateNoteTag(@Valid @RequestBody NoteTagSaveReqVO updateReqVO) {
        noteTagService.updateNoteTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除笔记标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('personal:note-tag:delete')")
    public CommonResult<Boolean> deleteNoteTag(@RequestParam("id") Integer id) {
        noteTagService.deleteNoteTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得笔记标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('personal:note-tag:query')")
    public CommonResult<NoteTagRespVO> getNoteTag(@RequestParam("id") Integer id) {
        NoteTagDO noteTag = noteTagService.getNoteTag(id);
        return success(BeanUtils.toBean(noteTag, NoteTagRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得笔记标签分页")
    @PreAuthorize("@ss.hasPermission('personal:note-tag:query')")
    public CommonResult<PageResult<NoteTagRespVO>> getNoteTagPage(@Valid NoteTagPageReqVO pageReqVO) {
        PageResult<NoteTagDO> pageResult = noteTagService.getNoteTagPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NoteTagRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出笔记标签 Excel")
    @PreAuthorize("@ss.hasPermission('personal:note-tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNoteTagExcel(@Valid NoteTagPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NoteTagDO> list = noteTagService.getNoteTagPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "笔记标签.xls", "数据", NoteTagRespVO.class,
                        BeanUtils.toBean(list, NoteTagRespVO.class));
    }

    @GetMapping("/getqrcode")
    @Operation(summary = "生成梯控二维码")
    @PermitAll
    @Parameter(name = "floorno", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('personal:note-tag:floorno')")
    public CommonResult<String> getqrcode(@RequestParam("floorno") String floorno,@RequestParam("floorType") String floorType,
                           HttpServletResponse response) {
        return success(noteTagService.getqrcode(floorno,floorType,response));
    }

    @GetMapping("/addVisitorPark")
    public CommonResult<String> addVisitorPark(@RequestParam("carNumber") String carNumber,@RequestParam("appointmentTime") String appointmentTime) {
        return success(noteTagService.addVisitorPark(carNumber,appointmentTime));
    }

}
